using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using Unity.Mathematics;
using UnityEngine;

public class EvilMage : MonoBehaviour
{
   public GameObject OrcPrefab;
   public GameObject BoxePrefab;

   private GameObject Orc;
   private GameObject Boxe;

   public Transform OrcStart;
   public Transform BoxeStart;

   public DialogueData_SO finalDialogueDataSo;

   private bool haveTalked;
   private bool haveCallBoxe;
   private Animator anim;
   public AudioSource AudioSource;

   public AudioClip battle;
   public AudioClip win;
   public bool haveEnd;

   private void Awake()
   {
      anim = gameObject.GetComponent<Animator>();
   }

   private void Update()
   {
      if (Orc != null)
      {
         if (Orc.GetComponent<CharacterStats>().characterData.currentHealth <50 && !haveCallBoxe)
         {
            anim.SetTrigger("Skill2");
            haveCallBoxe = true;
         }
      }

      if (Boxe != null)
      {
         if (Boxe.GetComponent<CharacterStats>().characterData.currentHealth <= 0)
         {
            if (!haveEnd)
            {
               gameObject.GetComponent<DialogueController>().dialogueDataSo = finalDialogueDataSo;
               gameObject.GetComponent<DialogueController>().OpenDialogue();
               GameManager.Instance.isEnd = true;
               QuestManager.Instance.updateQuestProgress(this.gameObject.name, 1);
               AudioSource.clip = win;
               AudioSource.Play();
               haveEnd = true;
            }
         }
      }
   }

   public void CallOrc()
   {
      Orc=Instantiate(OrcPrefab, OrcStart.position, Quaternion.identity);
   }

   public void CallBoxe()
   {
      Boxe=Instantiate(BoxePrefab, BoxeStart.position, Quaternion.identity);
   }

   private void OnTriggerEnter(Collider other)
   {
      if (other.gameObject.CompareTag("Player"))
      {
         if (!haveTalked)
         {
            gameObject.GetComponent<DialogueController>().OpenDialogue();
            anim.SetTrigger("Skill1");
            DOTween.To(() => transform.GetChild(1).GetComponent<SkinnedMeshRenderer>().materials[0].color, x => transform.GetChild(1).GetComponent<SkinnedMeshRenderer>().materials[0].color = x, new Color(1, 1, 1, 0), 3f);
            DOTween.To(() => transform.GetChild(2).GetComponent<SkinnedMeshRenderer>().materials[0].color, x => transform.GetChild(2).GetComponent<SkinnedMeshRenderer>().materials[0].color = x, new Color(1, 1, 1, 0), 3f);
            AudioSource.clip = battle;
            AudioSource.Play();
            haveTalked = true;
         }
      }
   }
   
   
   
}
